package cn.gailvlun.gll.presentation.login

import android.content.Context
import android.content.Intent
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.text.Html
import android.text.Spanned
import cn.gailvlun.gll.R
import cn.gailvlun.gll.databinding.ActivityRegisterBinding
import cn.gailvlun.gll.presentation.base.App
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.login.bean.CountryCode
import cn.gailvlun.gll.presentation.login.viewmodel.RegisterViewModel
import cn.gailvlun.gll.util.PhoneUtil
import cn.gailvlun.gll.util.SPUtil
import cn.gailvlun.gll.widget.edittext.FilterType


class RegisterActivity : BaseActivity() {

    companion object {
        @JvmStatic
        fun openActivity(context: Context) {
            context.startActivity(Intent(context, RegisterActivity::class.java))

        }
    }

    object Util {
        @JvmStatic
        fun getProtocol(): Spanned {
            return Html.fromHtml(App.getInstance().resources.getString(R.string.register_agree_protocol))
        }
    }

    private lateinit var mBinding: ActivityRegisterBinding
    private lateinit var mViewModel: RegisterViewModel

    override fun initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_register)
        mViewModel = RegisterViewModel(mContext, mBinding)
        mBinding.vm = mViewModel

        val countryCode = SPUtil.get(SPUtil.GLOBAL, "countryCode", CountryCode::class.java)
        if (countryCode == null) {
            mViewModel.countryNameAndCode = "CN +${PhoneUtil.getCountryCodeByDefault()}"
        } else {
            val indexStart = countryCode.countryName.indexOf("(")
            val indexEnd = countryCode.countryName.indexOf(")")
            mViewModel.countryNameAndCode = "${countryCode.countryName.substring(indexStart + 1, indexEnd).trim()} ${countryCode.countryCode}"
        }
    }

    override fun createHandler() {
        mBinding.nbv.setOnBackListener { onBackPressed() }
        mBinding.cetPhone.startFilter(FilterType.PHONE)
        mBinding.petPassword.startFilter()
    }

    override fun loadData(savedInstanceState: Bundle?) {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        mViewModel.onActivityResult(requestCode, resultCode, data)
    }

}




